<?php

include("utils.php");
include("lang_buildCustomNetwork.php");
if(!isset($_SESSION[siteuserId]))
echo '<script>location.href="index.php"</script>';
main_content("mainBuySpace();");
function mainBuySpace()
{
	if($_SESSION['langId']!=""){
		$langId=$_SESSION['langId'];
	}
	else{
		$langId="1";
	}
	
	$execCategory = mysql_query("select * from adcategories where status='Y' and langId='".$langId."'");
	$execCountry = mysql_query("select * from country where status='Y'");
	$sqlNetworkAds = "select * from ".NETWORKMASTER.",".NETWORKDETAILS." where ".NETWORKMASTER.".status='Y' AND ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId";
	$execNetworkAds = mysql_query($sqlNetworkAds);

	if(isset($_POST[btnSaveNetwork]))
	{
 		$val_category=$_POST['selCategory'];
 		$val_geography = $_POST['selGeography'];
		$all_geo_value = '';

		foreach($val_geography as $value_geography)
		{
			$all_geo_value = $all_geo_value.$value_geography.",";
		}

		$len_geo = strlen($all_geo_value);
		$final_geo_string = substr($all_geo_value,0,($len_geo-1));
		$all_category_value = '';
		foreach($val_category as $value_category)
		{
			$all_category_value = $all_category_value.$value_category.",";
		}

		$len_category = strlen($all_category_value);
		$final_category_string = substr($all_category_value,0,($len_category-1));
		$main_adgroup = "main_adgroup";

		$query_temp = " INSERT into temp_create_network(`identity`, `ad_type`, `bid`, `per`, `network`, `banner_size`, `category`, `country`, `userid`) values('".$main_adgroup."','".$_POST['adtype']."','".$_POST['max_bid']."','".$_POST['ad_unit']."','".$_POST['network']."','".$_POST['banner_size']."','".$final_category_string."','".$final_geo_string."',".$_SESSION['siteuserId'].") ";
		
		mysql_query($query_temp);
  			echo '<script>location.href="create_campaign.php"</script>';
		

		// 	if(isset($_POST['ron']) && $_POST['ron'] == 'ron')
		// 	{
		// 		echo '<script>location.href="create_campaign.php"</script>';
		// 	}
		// 
		// 	$val_category=$_POST['selCategory'];
		// 	$val_geography = $_POST['selGeography'];
		// 
		// 	$sqlTarget = "insert into adtarget_details set userId='".$_SESSION[siteuserId]."', adtargetType='".$_POST[adtype]."',adpriceUnit='".$_POST[ad_unit]."',adbidPrice='".$_POST[max_bid]."',targetedOn=now()";
		// 	mysql_query($sqlTarget);
		// 
		// 	$tid = mysql_insert_id();
		// 	
		// 	foreach($val_geography as $value_geography)
		// 	{
		// 		
		// 		$sqlNetworkLOC = "insert into adtarget_countries set adtargetId='".$tid."',countryId='".$value_geography."'";
		// 		mysql_query($sqlNetworkLOC);
		// 	}
		// 
		// 	foreach($val_category as $value_category)
		// 	{
		// 		$query_id = "select adcategoryId from adcategories where categoryName = '".$value_category."' ";
		// 		$res_id = mysql_query($query_id);
		// 		$row_id = mysql_fetch_array($res_id);
		// 		//echo "<br>"."Cat ID ---> ".$row_id['adcategoryId'];	
		// 		$sqlNetworkCategory = "insert into adtarget_categories set adtargetId='".$tid."',adcategoryId='".$row_id['adcategoryId']."'";
		// 		mysql_query($sqlNetworkCategory);
		// 	}
		//  	echo '<script>location.href="buying_campaign_manager.php"</script>';

	}
?>
<script type="text/javascript">
	function showHide(toShow,toHide)
	{
		document.getElementById(toShow).style.display='';
		document.getElementById(toHide).style.display='none';
		if(toShow=='tdCategory')
		{
			document.getElementById('passValue1').value = 'category_List';
			document.getElementById('passValue2').value =  'selCategory[]';
		}	
		else
		{
			document.getElementById('passValue1').value = 'geographyList';
			document.getElementById('passValue2').value =  'selGeography[]';
		}
	}
	
	function moveSelectedOptions(f, t, l, s, d)
	{
		var from = document.getElementById(f);
		var to = document.getElementById(t);
		var limit = 0;
		var sort = "text";
		if (s.length > 0)
		sort = s;
		var direction = d;
		if (l.length > 0)
		limit = parseInt(l);
		var msg = "";
		if (limit > 0 && to.options.length >= limit)
		{
			msg = "You have already selected the maximum number of options allowed.";
		}
		else
		{
			// Move them over
			var cnt = to.options.length;
			var selectAll = false;
			if (to.options.length == 1 && to.options[0].text.indexOf(" All ") > -1 &&
			direction == ">")
			selectAll = true;
			for (var i = 0; i < from.options.length; i++)
			{
				var o = from.options[i];
				if( (o.selected && cnt < limit) || (o.selected && limit == 0))
				{
					if (selectAll)
					{
						from.options[from.options.length] =
						new Option( to.options[0].text, to.options[0].value, false, false);
						to.options[0] = null;
						selectAll = false;
						cnt--;
					}
					
					if (o.text.indexOf(" All ") > -1 && direction == ">")
					{
						moveAllOptions(t, f);		// Block By ALOK
						//alert('a');
						to.options[to.options.length] = new Option( o.text, o.value, false, false);
						//to.options[to.options.length].selected = true;
						from.options[i] = null;
						from.selectedIndex = -1;
						cnt = limit;
						
					}
					else
					{
						//alert(to.name);
						to.options[to.options.length] = new Option( o.text, o.value, false, false);
						//to.options[i].selected = true;
						cnt++;
					}
				}
				else if (o.selected && limit > 0)
				{
					o.selected = false;
					if (msg.length == 0)
					msg = "You have exceeded the number of options allowed.  The following options from your selected list have been omitted:\n\n";
					msg += o.text + "\n";
				}
			}

			// Delete them from original
			for (var i = (from.options.length-1); i >= 0; i--)
			{
				var o = from.options[i];
				if (o.selected)
				{
					from.options[i] = null;
				}
			}
			sortSelect(from, sort);
			sortSelect1(to, sort);
			//from.selectedIndex = -1;
			// to.selectedIndex = -1;
			if(direction =='>'){
				from.selectedIndex = -1;
			}
			if(direction =='<'){
				to.selectedIndex = -1;
			}
	}
	if (msg.length > 0)
		alert(msg);
		//alert(document.getElementById(t).value);
		// to.selectedIndex=" All ";
		// to.selectedIndex=0;
		//document.getElementById('selCategory').selected;
 }

	function sortSelect1(obj, s)
	{
	 var o = new Array();
	 if (obj.options==null)
	 return;
	 for (var i = 0; i < obj.options.length; i++)
	 {
		 o[o.length] =
		 new Option(
		 obj.options[i].text,
		 obj.options[i].value,
		 obj.options[i].defaultSelected,
		 obj.options[i].selected);
	}
	
	if (o.length==0)
		return;
		o = o.sort(
		
	function(a,b)
	{
	 if (a.text+"" == "United States") { return -1; }
	 else if (b.text+"" == "United States") { return 1; }
	 else if (a.value == 9999) { return -1; }
	 else if (b.value == 9999) { return 1; }
	if (s == "text")
	{
	 if ((a.text+"") < (b.text+"")) { return -1; }
	 if ((a.text+"") > (b.text+"")) { return 1; }
	}
	else if (s == "value")
	{
	 if ((a.value) < (b.value)) { return -1; }
	 if ((a.value) > (b.value)) { return 1; }
	}
	return 0;
 });
 for (var i = 0; i < o.length; i++)
 {

 obj.options[i] =
 new Option(o[i].text,
 o[i].value,
 o[i].defaultSelected,
 o[i].selected);
	
	if(!obj.options[i].selected) 
	{
		obj.options[i].selected=true;
		
	}
 }
}
function sortSelect(obj, s)
{
 var o = new Array();
 if (obj.options==null)
 return;
 for (var i = 0; i < obj.options.length; i++)
 {
 o[o.length] =
 new Option(
 obj.options[i].text,
 obj.options[i].value,
 obj.options[i].defaultSelected,
 obj.options[i].selected);
 }
 if (o.length==0)
 return;
 o = o.sort(
 function(a,b)
 {
 if (a.text+"" == "United States") { return -1; }
 else if (b.text+"" == "United States") { return 1; }
 else if (a.value == 9999) { return -1; }
 else if (b.value == 9999) { return 1; }
  if (s == "text")
 {
 if ((a.text+"") < (b.text+"")) { return -1; }
 if ((a.text+"") > (b.text+"")) { return 1; }
 }
 else if (s == "value")
 {
 if ((a.value) < (b.value)) { return -1; }
 if ((a.value) > (b.value)) { return 1; }
 }
 return 0;
 });
 for (var i = 0; i < o.length; i++)
 {
	
 obj.options[i] =
 new Option(o[i].text,
 o[i].value,
 o[i].defaultSelected,
 o[i].selected);
if(!obj.options[i].selected) 
	{
		obj.options[i].selected=true;
		
	}
 }
}


function disable_unable()
{
	if(document.getElementById('ron').checked == true) {
	
	document.getElementById('category_List').disabled = true;

	document.getElementById('geographyList').disabled = true;

							   }

	else						   {
	
	document.getElementById('category_List').disabled = false;

	document.getElementById('geographyList').disabled = false;

							   }
}


function confirm_msg(formID)
{ 

	if (document.getElementById(formID)) {
		var form = document.getElementById(formID);
	} else {
		return;
	}

	// CONFIRM REQUIRES ONE ARGUMENT
	var message = "Please confirm it is OK to proceed...";

	// CONFIRM IS BOOLEAN. THAT MEANS THAT
	// IT RETURNS TRUE IF 'OK' IS CLICKED
	// OTHERWISE IT RETURN FALSE
	var return_value = confirm(message);
	// DISPLAY THE RETURNED VALUE IN THE
	// CONFIRM POPUP DEMO TEXT INPUT BOX
	//form.return_value.value = return_value;

	// TEST TO SEE IF TRUE|FALSE RETURNED
	if ( return_value === true ) {
		// YOUR 'OK' SCRIPT GOES HERE

 		//window.location="http://esolz.org/admobweb/create_campaign.php";
		setTimeout("redirect()",500);

		
		
	} else {
		// YOUR 'CANCEL' SCRIPT GOES HERE
		return false;
	}

}

function redirect()
{
window.location = 'create_campaign.php';
}

function type_setting()
{

	if(document.getElementById('adtype').value == 'text')
	{
	document.getElementById('adUnit').value = 'CPC';
	document.getElementById('banner_size').style.display = 'none';

	//document.all.selectID.selectedIndex = value // IE only
	}

	if(document.getElementById('adtype').value == 'thumbnail')
	{
	document.getElementById('adUnit').value = 'CPC';
	document.getElementById('banner_size').style.display = 'none';

	}

	if(document.getElementById('adtype').value == 'banner')
	{
	document.getElementById('adUnit').value = 'CPM';
	document.getElementById('banner_size').style.display = 'block';
	}
 
}

	function checkform()
	{
		document.getElementById('block1').style.display='none';
	document.getElementById('block2').style.display='none';
		if (document.getElementById('selCategory[]').value=="")
		{
			document.getElementById('block1').style.display='block';
			return false;
		}
		
		if (document.getElementById('selGeography[]').value=="")
		{
			document.getElementById('block2').style.display='block';
			return false;
		}		
		
		return true;
	}

</script>

<input type="hidden" name="hidAdSpaceId" id="hidAdSpaceId" value="<?php echo $adspaceID;?>">
<input type="hidden" name="hidDelAdSpaceId" id="hidDelAdSpaceId" value="<?php echo $fetFavId[0];?>">

<div id="control" style="width: auto; padding-top: 5px; padding-bottom: 15px;">
	<table width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
            <td valign="top" align="center">
				<table width="98%" border="0" align="center" cellpadding="5" cellspacing="5">
					<tr>
						<td width="20%" valign="top"><?php include("left_pannel.php"); ?></td>
						<td width="10px">&nbsp;</td>
						<td valign="top">
							<table width="100%" border="0" cellspacing="0" cellpadding="0">
								<tr>
									<td valign="top">
										<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
											<tr>
												<td class="BoxContent" valign="top">
													<table width="827" border="0" cellpadding="0" cellspacing="0" class="center">
														<tr>
															<td><img src="images/red_table_ltop.jpg" alt="" /></td>
															<td class="red_table_top"></td>
															<td><img src="images/red_table_rtop.jpg" alt="" /></td>
														</tr>
														<tr>
															<td class="red_table_LSide"></td>
															<!-- ban body part -->
															<td class="inner_contentArea" align="center">
																<form name="frmTargets" method="POST" action="buildCustomNetwork.php" id="frmTargets" onsubmit="return checkform();">
																	<input type="hidden" name="passValue1" id="passValue1" value="category_List">
																	<input type="hidden" name="passValue2" id="passValue2" value="selCategory[]">
																	<table width="750" cellspacing="0" cellpadding="0" border="0">
																		<TR>
																		  <TD align="center" class="sky font-18"><?php echo CUSTOMNETWORKTARGETING; ?></TD>
																		</TR>
																		<TR>
																		  <TD align="left">&nbsp;<?php echo CUSTOMNETWORKMSG; ?></TD>
																		</TR>
																		<TR>
																		  <TD align="left">&nbsp;</TD>
																		</TR>
																		<TR>
																		  <TD align="left">&nbsp;</TD>
																		</TR>
																		<TR>
																			<TD align="left">
																				<table width="100%" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
																					<TR>
																						<TD>&nbsp;</TD>
																					</TR>
																					<TR>
																						<TD>
																							<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
																								<TR>
																									<TD width="258" align="left"><strong>Ad Type</strong>&nbsp;
																										<select id="adtype" name="adtype" class="InputBox" style="width:150px;" onchange="return type_setting();">
																										  <option value="text">Text</option>
																										  <option value="thumbnail">Thumbnail</option>
																										  <option value="banner">Banner</option>
																										</select>  
																									</TD>
																									<TD align="left" width="39">&nbsp;</TD>
																									<TD width="241" align="left"><strong>Bid</strong>&nbsp;<strong>$</strong>
																										<input id="maxBidInput" name="max_bid" style="margin-left: 5px; width: 50px;" type="text" class="InputBox" value="0.5">
																										&nbsp; <strong>Per</strong>&nbsp;
																										<input type="text" readonly="true" id="adUnit" name="ad_unit" class="InputBox" style="width:50px;" value="CPC">
																									</TD>
																									<TD width="158"><strong>Network</strong>&nbsp;
																										<select id="network" name="network" class="InputBox" style="width:150px;">
																										  <option label="CPC" value="cpc">Custom</option>
																										  <?php
																										while($fetNetworkAds = mysql_fetch_array($execNetworkAds))
																										{

																											?>
																										  <option label="CPV" value="<?php echo $fetNetworkAds[networkId]; ?>"><?php echo strtoupper($fetNetworkAds[networkType]); ?> - <?php echo ucfirst($fetNetworkAds[networkName]); ?> ( $<?php echo $fetNetworkAds[networkMinPrice].' '.$fetNetworkAds[networkBidType]; ?>)</option>
																										<?php 
																										}
																										?>
																										</select>
																									</TD>
																								</TR>
																								<tr>
																									<td id="banner_size" style="display:none" nowrap="nowrap"><strong>Banner Size</strong> &nbsp;
																										<seleCT name="banner_size">
																											<opTION value="no_banner">Select</opTION>
																											<opTION value="120x240">120x240</opTION>
																											<opTION value="120x400">120x400</opTION>
																											<opTION value="120x600">120x600</opTION>
																											<opTION value="125x125">125x125</opTION>
																											<opTION value="150x600">150x600</opTION>
																										</seleCT>
																									</td>
																									<TD align="left"><input type="checkbox" name="ron" id="ron" onclick="return disable_unable();" value="ron">RON</td>
																								</tr>

																							</table>
																						</TD>
																					</TR>
																					<TR>
																						<TD align="left">
																							<table width="100%" border="0" cellpadding="0" cellspacing="0">
																								<TR>
																									<TD align="center">&nbsp;</td>
																								</tr>
																								<TR>
																									<TD align="left">
																										<table width="100%" border="0" cellspacing="0" cellpadding="0">
																											<tr>
																												<td width="8">&nbsp;</td>
																													<td>
																														<table width="100%" border="0" cellspacing="0" cellpadding="0">
																															<tr>
																																<td width="200" valign="top">
																																	<table width="100%" border="0" cellspacing="0" cellpadding="0">
																																		<tr>
																																			<td height="25" style=" font-family: Arial; font-size: 12px; font-weight: bold; color: #000000;">Targeting Options <a href="#" style="font-family: Arial; font-size: 12px; text-decoration: underline;">What's this? </a></td>
																																		</tr>
																																		<tr>
																																			<td>
																																				<table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
																																					<tr>
																																						<td align="left" class="listHeader" style="padding-left: 6px;cursor:pointer;" onclick="showHide('tdCategory','tdGeography')"><span onclick="">Category</span></td>
																																					</tr>
																																					<tr>
																																						<td style="padding-left: 10px;" id="tdCategory">
																																							<select multiple="multiple" id="category_List" name="category_List" style="border:none; height:100px;width: 195px;" size="6">
																																								<?php 
																																								while($fetchCategory = mysql_fetch_array($execCategory))
																																								{
																																								?>
																																									<option value="<?php echo $fetchCategory['categoryName']; ?>" ><?php echo $fetchCategory['categoryName']; ?></option>
																																	
																																								<?php
																																								}
																																								?>
																																							</select>							
																																						</td>
																																					</tr>
																																				</table>
																																			</td>
																																		</tr>
																																		<tr>
																																			<td height="8px;"></td>
																																		</tr>
																																		<tr>
																																			<td>
																																				<table width="200" border="0" cellspacing="0" cellpadding="0" class="outlineBorder">
																																					<tr>
																																						<td align="left" class="listHeader" style="padding-left: 6px;cursor:pointer;" onclick="showHide('tdGeography','tdCategory')">Geography</td>
																																					</tr>
																																					<tr>
																																						<td style="padding-left: 10px;display:none;" id="tdGeography">
																																							<select multiple="multiple" id="geographyList" name="geographyList" style="border:none; height:100px;width: 195px;" size="6">
																																								<?php 
																																								while($fetchcountry = mysql_fetch_array($execCountry))
																																								{
																																								?>
																																											<option value="<?php echo $fetchcountry['countryId']; ?>" ><?php echo $fetchcountry['countryName']; ?></option>
																																												<?php
																																								}
																																								?>
																																							</select>										
																																						</td>
																																					  </tr>
																																				</table>
																																			</td>
																																		</tr>
																																		<tr>
																																			<td height="8px;"></td>
																																		</tr>
																																		<tr>
																																			<td>&nbsp;</td>
																																		</tr>
																																		<tr>
																																			<td>&nbsp;</td>
																																		</tr>
																																	</table>
																																</td>
																																<td width="100" align="center"><input type="button" name="Submit" value="Add&gt;&gt;" style="width: 80px;" onclick="moveSelectedOptions(document.getElementById('passValue1').value, document.getElementById('passValue2').value, '5', '','>');" /><br /><input onclick="moveSelectedOptions(  document.getElementById('passValue2').value, document.getElementById('passValue1').value, '', '','<');" type="button" name="Submit" value="&lt;&lt;Remove" style="width: 80px;" /></td>
																																<td valign="top">
																																	<table width="100%" border="0" cellspacing="0" cellpadding="0">
																																		<tr>
																																		  <td height="25" style=" font-family: Arial; font-size: 12px; font-weight: bold; color: #000000;">Your Targets </td>
																																		</tr>
																																		<tr>
																																		  <td>
																																			<table width="100%" border="0" cellspacing="0" cellpadding="0">
																																				<tr>
																																				  <td width="49%" valign="top">
																																					<table width="100%" border="0" cellspacing="0" cellpadding="0">
																																						<tr>
																																							<td>
																																								<table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
																																									<tr>
																																									  <td align="left" class="listHeader" style="padding-left: 6px;">Category</td>
																																									</tr>
																																									<tr>
																																									  <td style="padding-left: 10px;">
																																										 <select multiple="multiple" style="border:none; height:90px;width: 190px;" id="selCategory[]" name="selCategory[]" size="6" > </select>
																																										&nbsp;</td>
																																									</tr>
																																									
																																								</table>
																																							</td>
																																						</tr>
																																						<tr style="display:none;" id='block1'>
																																							<td style="height:25px;color:red;font-size:12px;">Please fill in the field: Category</td>
																																						</tr>
																																						<tr>
																																						  <td height="8px;"></td>
																																						</tr>
																																						<!--<tr>
																																						  <td><table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
																																							<tr>
																																							  <td align="left" class="listHeader" style="padding-left: 6px;">Site</td>
																																							</tr>
																																							<tr>
																																							  <td style="padding-left: 10px;">&nbsp;</td>
																																							</tr>
																																						  </table></td>
																																						</tr>-->
																																					</table>
																																			
																																				</td>
																																				<td width="2%" valign="top">&nbsp;</td>
																																				<td width="49%" valign="top">
																																					<table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
																																						<tr>
																																							<td align="left" class="listHeader" style="padding-left: 6px;">Geography</td>
																																						</tr>
																																						<tr>
																																							<td style="padding-left: 10px;"><select multiple="multiple" style="border:none; height:100px;width: 190px;" id="selGeography[]" name="selGeography[]" size="6">
																																								</select></td>
																																						</tr>
																																						
																																					</table>
																																					<table width="100%">
																																						<tr style="display:none;" id='block2'>
																																														<td style="height:25px;color:red;font-size:12px;">Please fill in the field: Geography</td>
																																													</tr>
																																					</table>
																																				</td>
																																			</tr>
																																		</table>
																																	</td>
																																</tr>
																															</table>
																														</td>
																													</tr>
																												</table>
																											</td>
																											<td width="8">&nbsp;</td>
																										</tr>
																									</table>
																								</td>
																							</tr>
																							<TR>
																							  <TD align="right"><input type="submit" value="Save" name="btnSaveNetwork" />&nbsp;<input type="submit" value="Cancel" onclick="return confirm_msg('frmTargets');" />&nbsp;</td>
																							</tr>
																							<TR>
																							  <TD align="right">&nbsp;</td>
																							</tr>
																						</table>
																					</TD>
																				</TR>
																			</table>
																		</TD>
																	</TR>
																</table>
															</form>
														</TD>
														<td class="red_table_RSide"></td>
													</tr>
													<tr>
													  <td><img src="images/red_table_lbottom.jpg" alt="" /></td>
													  <td class="red_table_bottom"></td>
													  <td><img src="images/red_table_rbottom.jpg" alt="" /></td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
							</tr>
						</table>
					</td>
					
				</tr>
			</table>	
		</td>
	</tr>
</table>
			
<?php
  }
?> 